package com.fifth.home.controller;

import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Banner;
import com.fifth.common.pojo.entity.Comment;
import com.fifth.common.pojo.entity.Zone;
import com.fifth.home.service.IBannerService;
import com.fifth.home.utils.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author 第五组
 * @since 2022-09-01
 */
@RestController
@RequestMapping("/home/banner")
@Api(tags = "轮播相关的接口")
public class BannerController {
    @Autowired
    private IBannerService bannerService;

    /**
     * 前端轮播
     *
     * @return
     */
    @ApiOperation(value = "查询前端轮播显示")
    @GetMapping("rotationalSeeding")
    public CommonResult rotationalSeeding() {
        return bannerService.selectRotational();
    }


    /**
     * 轮播 模糊查询
     *
     * @param banner
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "后台轮播查询")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "currentPage", value = "当前页码", dataType = "int"),
                    @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int"),
                    @ApiImplicitParam(name = "banner", value = "轮播实体类", dataType = "Banner")
            }
    )
    @PostMapping("rotationSearch/{currentPage}/{pageSize}")
    public CommonResult fuzzySearch(@RequestBody Banner banner, @PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        return bannerService.fuzzyRotationQuery(banner, currentPage, pageSize);
    }

    /**
     * 添加轮播
     *
     * @param banner
     * @return
     */
    @ApiOperation(value = "后台轮播添加")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "banner", value = "轮播实体类", dataType = "Banner")
            }
    )
    @PostMapping("addBanner")
    public CommonResult addBanner(@RequestBody Banner banner) {
        return bannerService.insertBanner(banner);
    }

    /**
     * 图片上传OSS
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "后台轮播图片上传")
    public CommonResult upload01(MultipartFile file) {
        try {
            String upload = OSSUtil.upload(file);
            return new CommonResult(2000, "上传成功", upload);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new CommonResult(5000, "上传失败", null);

    }

    /**
     * 修改轮播状态
     *
     * @param id
     * @param isDisable
     * @return
     */
    @ApiOperation("修改轮播状态")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "轮播id属性", dataType = "String"),
                    @ApiImplicitParam(name = "isDisable", value = "轮播状态 0正常 1禁用", dataType = "String")
            }
    )
    @PostMapping("changeStatus/{id}/{isDisable}")
    public CommonResult updateDisable(@PathVariable String id, @PathVariable String isDisable) {
        return bannerService.updateStatus(id, isDisable);
    }

    /**
     * 删除轮播  逻辑删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除轮播")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "id属性", dataType = "String")
            }
    )
    @GetMapping("deleteBanner/{id}")
    public CommonResult deleteBanner(@PathVariable String id) {
        return bannerService.deleteBanner(id);
    }


    /**
     * 修改轮播
     *
     * @param banner
     * @return
     */
    @ApiOperation(value = "后台轮播修改")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "banner", value = "轮播实体类", dataType = "Banner")
            }
    )
    @PostMapping("editBanner")
    public CommonResult editBanner(@RequestBody Banner banner) {
        return bannerService.updateBanner(banner);
    }
}

